import Vue from 'vue'
import VueRouter from 'vue-router'
// var VueCookie = require('vue-cookie')
import Index from '~components/index'
import util from './lib/util'
import Vip from '~components/vip'
import Error from '~components/mixins/no-data.vue'
import clear from '~components/clear.vue'
import InletShopOk from '~components/pay-ok'
import InletShopError from '~components/pay-error'
import api from '~api/index'
const Account = resolve => {
  require.ensure(['~components/account'], () => {
    resolve(require('~components/account'))
  })
}
const Help = resolve => {
  require.ensure(['~components/help'], () => {
    resolve(require('~components/help'))
  })
}
const AccountEdit = resolve => {
  require.ensure(['~components/account-edit'], () => {
    resolve(require('~components/account-edit'))
  })
}
const Member = resolve => {
  require.ensure(['~components/member'], () => {
    resolve(require('~components/member'))
  })
}
const Coupon = resolve => {
  require.ensure(['~components/coupon'], () => {
    resolve(require('~components/coupon'))
  })
}
const RefuelingList = resolve => {
  require.ensure(['~components/refueling-list'], () => {
    resolve(require('~components/refueling-list'))
  })
}
const RefuelingShow = resolve => {
  require.ensure(['~components/refueling-show'], () => {
    resolve(require('~components/refueling-show'))
  })
}
const vipCrad = resolve => {
  require.ensure(['~components/vip-card'], () => {
    resolve(require('~components/vip-card'))
  })
}
const vipCradShow = resolve => {
  require.ensure(['~components/vip-card-show'], () => {
    resolve(require('~components/vip-card-show'))
  })
}
const vipIntearal = resolve => {
  require.ensure(['~components/vip-integral'], () => {
    resolve(require('~components/vip-integral'))
  })
}
const vipIntearalList = resolve => {
  require.ensure(['~components/vip-integral-list'], () => {
    resolve(require('~components/vip-integral-list'))
  })
}
const vipIntearalShow = resolve => {
  require.ensure(['~components/vip-integral-show'], () => {
    resolve(require('~components/vip-integral-show'))
  })
}
const vipRecharge = resolve => {
  require.ensure(['~components/vip-recharge'], () => {
    resolve(require('~components/vip-recharge'))
  })
}
const vipRechargeList = resolve => {
  require.ensure(['~components/vip-recharge-list'], () => {
    resolve(require('~components/vip-recharge-list'))
  })
}
const vipCoupon = resolve => {
  require.ensure(['~components/vip-coupon'], () => {
    resolve(require('~components/vip-coupon'))
  })
}
const Inlet = resolve => {
  require.ensure(['~components/inlet'], () => {
    resolve(require('~components/inlet'))
  })
}
const InletShop = resolve => {
  require.ensure(['~components/inlet-shop'], () => {
    resolve(require('~components/inlet-shop'))
  })
}
const gasstationList = resolve => {
  require.ensure(['~components/gasstation-list'], () => {
    resolve(require('~components/gasstation-list'))
  })
}
const gasstationShow = resolve => {
  require.ensure(['~components/gasstation-show'], () => {
    resolve(require('~components/gasstation-show'))
  })
}
const routes = [
  {
    path: '/',
    component: Index,
    meta: {
      title:'',
    }
  },
  {
    path: '/vip',
    component: Vip,
    meta: {
      title:'个人中心',
      requiresAuth: true
    }
  },
  {
    path: '/help',
    component: Help,
    meta: {
      title:'帮助中心',
      requiresAuth: true
    }
  },
  {
    path: '/account',
    component: Account,
    meta: {
      title:'帐号安全',
      requiresAuth: true
    }
  },
  {
    path: '/account/edit',
    component: AccountEdit,
    meta: {
      title:'修改手机',
      requiresAuth: true
    }
  },
  {
    path: '/member',
    component: Member,
    meta: {
      title:'资料修改',
      requiresAuth: true
    }
  },
  {
    path: '/coupon',
    component: Coupon,
    meta: {
      title:'优惠劵',
      requiresAuth: true
    }
  },
  {
    path: '/refueling',
    component: RefuelingList,
    meta: {
      title:'加油记录',
      requiresAuth: true
    }
  },
  {
    path: '/refueling/show',
    component: RefuelingShow,
    meta: {
      title:'加油记录详情',
      requiresAuth: true
    }
  },
  {
    path: '/card',
    component: vipCrad,
    meta: {
      title:'会员卡',
      requiresAuth: true
    }
  },
  {
    path: '/card/show',
    component: vipCradShow,
    meta: {
      title:'会员中心',
      requiresAuth: true
    }
  },
  {
    path: '/integral',
    component: vipIntearal,
    meta: {
      title:'积分兑换',
      requiresAuth: true
    }
  },
  {
    path: '/integral/list',
    component: vipIntearalList,
    meta: {
      title:'积分兑换',
      requiresAuth: true
    }
  },
  {
    path: '/integral/show',
    component: vipIntearalShow,
    meta: {
      title:' 积分兑换详情',
      requiresAuth: true
    }
  },
  {
    path: '/recharge',
    component: vipRecharge,
    meta: {
      title:' 余额充值',
      requiresAuth: true
    }
  },
  {
    path: '/recharge/list',
    component: vipRechargeList,
    meta: {
      title:' 余额列表',
      requiresAuth: true
    }
  },
  {
    path: '/coupon',
    component: vipCoupon,
    meta: {
      title:' 添加优惠劵',
      requiresAuth: true
    }
  },
  {
    path: '/inlet',
    component: Inlet,
    meta: {
      title:'选择油枪',
      requiresAuth: true
    }
  },
  {
    path: '/pay',
    component: InletShop,
    meta: {
      title:'订单确认',
      requiresAuth: true
    }
  },
  {
    path: '/payok',
    component: InletShopOk,
    meta: {
      title:'支付成功',
      requiresAuth: true
    }
  },
  {
    path: '/payerror',
    component: InletShopError,
    meta: {
      title:'购买失败'
    }
  },
  {
    path: '/gasstation',
    component: gasstationList,
    meta: {
      title:'油站列表'
    }
  },
  {
    path: '/gassshow',
    component: gasstationShow,
    meta: {
      title:'油站详情'
    }
  },
  {
    path: '/clear',
    component: clear,
    meta: {
      title:'清除缓存'
    }
  },
  {
    path: '*',
    component: Error,
    meta: {
      title:'404错误'
    }
  }
]
Vue.use(VueRouter)
let router = new VueRouter({
  mode: 'history',
  routes
})
router.beforeEach((to, from, next) => {
  let openId = util.localItem.get('openid') // 取openid
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!openId) {
      next({
        path: '/'
      })
    } else {
      next()
    }
  } else {
    next() // 确保一定要调用 next()
  }
})

// //判断token是否有效
// var isValidToken = function(){
//   let token = util.localItem.get('token')         // 设置token
//   let expires_in = util.localItem.get('expires_time')  // 设置token过期时间
//   if(!token || !expires_in){
//     return false;
//   }
//   var now = (new Date().getTime());
//   if (now < expires_in) { //判断当前时间是否小于 过期时间
//     return true;
//   }else{
//     return false;
//   }
// }
// //获取token
// async function getToken () {
//   let _token = util.localItem.get('token') // 获取token
//   if (_token && isValidToken()){
//     return _token
//   } else {
//     const { data: { data, code } } = await api.post('/v1/getToken', {carutoo_appid: 100001, secret_key: '40ST5V69JC2D7QPAYIW38MXHFRNZLOGUB1KE'})
//     if (data && code === '200') {
//        var now = (new Date().getTime());
//       var expires_in = now + (data.expires_time - 20)*1000
//        util.localItem.set('token',data.token) // 设置token
//        util.localItem.set('expires_time',expires_in)  // 设置token过期时间
//       return data.token
//     }
//   }
// }
module.exports = router
